----------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    梅花酿：
    配方：含有梅花，冰，至少0.5糖度，不能有树枝，肉，蛋，优先度30，烹饪时间154秒，最大堆叠7
    效果：是一种酒，保质期14天，放在地上腐烂速度为2倍。可以放入乾坤泰卦帽，不能放进盐盒。
    饮用后-7饱度，+14san，+14HP，+7体温，增加一层“酗酒”。喝酒最高只能让体温提升到70度。
    酗酒状态持续2分钟，每层令体感温度+7度，-21san/min，且获得0.28火伤系数。
    酗酒状态下重复饮酒会刷新酗酒状态为2分钟并增加一层酗酒
    比如连喝5瓶，就是-105san/min体感温度+35度同时获得1.4火伤系数，接着持续两分钟。
    燃烧效率等同于木板。饮用有个时间略长的动画效果（肯定不会原创），不可提前取消。
    无层数时饮用有台词：
    有层数时饮用有台词：
    梅花酿的“酗酒”效果结束有台词：
    “酗酒”层数过高时会有醉酒的特殊动作（把表情锁定为特殊的酗酒表情）

]]--
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 素材
    local assets = {
        Asset("ANIM", "anim/hutao_cooked_food_plum_blossom_wine.zip"), 
        Asset( "IMAGE", "images/inventoryimages/hutao_cooked_food_plum_blossom_wine.tex" ),  -- 背包贴图
        Asset( "ATLAS", "images/inventoryimages/hutao_cooked_food_plum_blossom_wine.xml" ),
    }
----------------------------------------------------------------------------------------------------------------------------------------------------
--- 食物本体
    local function PerishMultTask(inst)
        if inst.components.inventoryitem.owner == nil then
            inst.components.perishable.localPerishMultiplyer = 2
        else
            inst.components.perishable.localPerishMultiplyer = 1
        end
    end
    local function food_fn()

        local inst = CreateEntity() -- 创建实体
        inst.entity:AddTransform() -- 添加xyz形变对象
        inst.entity:AddAnimState() -- 添加动画状态
        inst.entity:AddNetwork() -- 添加这一行才能让所有客户端都能看到这个实体

        MakeInventoryPhysics(inst)

        inst.AnimState:SetBank("hutao_cooked_food_plum_blossom_wine") -- 地上动画
        inst.AnimState:SetBuild("hutao_cooked_food_plum_blossom_wine") -- 材质包，就是anim里的zip包
        inst.AnimState:PlayAnimation("idle") -- 默认播放哪个动画

        MakeInventoryFloatable(inst)
        inst:AddTag("preparedfood")

        inst.entity:SetPristine()
        
        if not TheWorld.ismastersim then
            return inst
        end
        --------------------------------------------------------------------------
        ------ 物品名 和检查文本
        inst:AddComponent("inspectable")

        inst:AddComponent("inventoryitem")
        -- inst.components.inventoryitem:ChangeImageName("leafymeatburger")
        inst.components.inventoryitem.imagename = "hutao_cooked_food_plum_blossom_wine"
        inst.components.inventoryitem.atlasname = "images/inventoryimages/hutao_cooked_food_plum_blossom_wine.xml"

        --------------------------------------------------------------------------


        inst:AddComponent("edible") -- 可食物组件
        inst.components.edible.foodtype = FOODTYPE.GOODIES
        inst.components.edible:SetOnEatenFn(function(inst,eater)
            if eater and eater:HasTag("hutao") then
                --------------------------------------------------------------------------------------------
                --- 添加debuff
                    local debuff_prefab = "hutao_debuff_plum_blossom_wine"
                    local debuff_inst = nil
                    local test_num = 100
                    while test_num > 0 do
                        eater:AddDebuff(debuff_prefab,debuff_prefab)
                        debuff_inst = eater:GetDebuff(debuff_prefab)
                        if debuff_inst and debuff_inst:IsValid() then
                            break
                        end
                        test_num = test_num - 1
                    end
                --------------------------------------------------------------------------------------------
                --- debuff 层数
                    local debuff_level = 0
                    if debuff_inst and debuff_inst:IsValid() then
                        debuff_level = debuff_inst.components.hutao_data:Add("level",0)
                    end
                --------------------------------------------------------------------------------------------
                --- 台词控制器
                    if math.random() < 0.3 then
                        eater.components.hutao_com_dialogue:Say("eaten.plum_blossom_wine")
                    end
                --------------------------------------------------------------------------------------------
            end
        end)
        inst.components.edible.hungervalue = -7
        inst.components.edible.sanityvalue = 14
        inst.components.edible.healthvalue = 14

        inst:AddComponent("perishable") -- 可腐烂的组件
        inst.components.perishable:SetPerishTime(14*TUNING.PERISH_ONE_DAY)
        inst.components.perishable:StartPerishing()
        inst.components.perishable.onperishreplacement = "spoiled_food" -- 腐烂后变成腐烂食物
        inst:DoPeriodicTask(1,PerishMultTask)

        inst:AddComponent("stackable") -- 可堆叠
        inst.components.stackable.maxsize = TUNING.STACK_SIZE_LARGEITEM
        inst:AddComponent("tradable")

        MakeHauntableLaunch(inst)
        -------------------------------------------------------------------
        --- 落水影子
            local function shadow_init(inst)
                if inst:IsOnOcean(false) then       --- 如果在海里（不包括船）
                    inst.AnimState:Hide("SHADOW")
                else                                
                    inst.AnimState:Show("SHADOW")
                end
            end
            inst:ListenForEvent("on_landed",shadow_init)
            shadow_init(inst)
        -------------------------------------------------------------------
        
        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------
--- debuff
    local DEBUFF_TIMER_DELTA = 0.5              -- debuff 刷新检查时间
    local DEBUFF_REFRESH_TIME = 2*60               -- debuff 时间
    local DEBUFF_MAX_TEMPERATURE = 70          -- DEBUFF能造成的体温上限
    local DEBUFF_TEMPERATURE_UP_PER_SECOND = 0.1  -- DEBUFF每秒上升的温度

    local function OnAttached(inst,target) -- 玩家得到 debuff 的瞬间。 穿越洞穴、重新进存档 也会执行。
        inst.entity:SetParent(target.entity)
        -- inst.Network:SetClassifiedTarget(target)
        inst.Transform:SetPosition(0,0,0)
        inst.player = target
        -----------------------------------------------------
        --- 控制器检查
            if target.components.hutao_com_sanity_custom_rate == nil
                or target.components.hutao_com_fire_damage == nil then
                inst:Remove()
                print("error 梅菜酿 debuff 控制器缺失")
                return
            end
        -----------------------------------------------------
        --- 计时器初始化
            if inst.components.hutao_data:Add("time",0) == 0 then
                inst.components.hutao_data:Set("time",DEBUFF_REFRESH_TIME)
                target.components.hutao_com_inspect_data:Set("hutao_debuff_plum_blossom_wine",DEBUFF_REFRESH_TIME)
            end
            inst:DoPeriodicTask(DEBUFF_TIMER_DELTA,function()
                local ret_time = inst.components.hutao_data:Add("time",-DEBUFF_TIMER_DELTA)
                target.components.hutao_com_inspect_data:Set("hutao_debuff_plum_blossom_wine",ret_time)
                if ret_time <= 0 then
                    inst:Remove()
                    if target.components.hutao_com_dialogue then
                        target.components.hutao_com_dialogue:Say("eaten.debuff_remove.plum_blossom_wine")
                    end
                end
                -- print("梅菜酿debuff剩余时间：",level,ret_time)
            end)
            inst:ListenForEvent("onremove",function()
                target.components.hutao_com_inspect_data:Set("hutao_debuff_plum_blossom_wine",0)                
            end)
        -----------------------------------------------------
        --- 体温
            if target.components.temperature then
                inst:DoPeriodicTask(1,function()
                    local level = inst.components.hutao_data:Add("level",0)
                    local current_temp = target.components.temperature.current
                    if current_temp < DEBUFF_MAX_TEMPERATURE then
                        target.components.temperature:DoDelta(DEBUFF_TEMPERATURE_UP_PER_SECOND*level)
                    end
                end)
            end
        -----------------------------------------------------
        --- 等级刷新
            inst:ListenForEvent("refresh_debuff",function(inst)
                --- 等级从1开始。
                local level = inst.components.hutao_data:Add("level",0)
                if level == 0 then
                    level = inst.components.hutao_data:Add("level",1)
                end
                --- San控制器
                local sanity_rate = -21*level
                target.components.hutao_com_sanity_custom_rate:AddRatePerMin(inst,sanity_rate)
                --- 火伤系数（加法）
                local fire_damage = 0.28*level
                target.components.hutao_com_fire_damage:AddAdditiveMultiplier(inst,fire_damage)
            end)
        -----------------------------------------------------
        --- 加载初始化。
            inst:DoTaskInTime(0,function()
                inst:PushEvent("refresh_debuff")
            end)
            inst:PushEvent("refresh_debuff")
        -----------------------------------------------------
    end

    local function ExtendDebuff(inst)
        inst.components.hutao_data:Add("level",1)
        inst.components.hutao_data:Set("time",DEBUFF_REFRESH_TIME)
        inst:PushEvent("refresh_debuff")
    end

    local function debuff_fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddNetwork()
        inst.entity:AddTransform()

        inst:AddTag("CLASSIFIED")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        inst:AddComponent("hutao_data")
        inst:AddComponent("debuff")
        inst.components.debuff:SetAttachedFn(OnAttached)
        inst.components.debuff.keepondespawn = true -- 是否保持debuff 到下次登陆
        inst.components.debuff:SetExtendedFn(ExtendDebuff)

        return inst
    end
----------------------------------------------------------------------------------------------------------------------------------------------------

return Prefab("hutao_cooked_food_plum_blossom_wine", food_fn, assets),
    Prefab("hutao_debuff_plum_blossom_wine", debuff_fn, assets)